// HOC高阶组件：本质上就是一个函数。
// HOC函数需要接收一个组件作为参数，在HOC中对该组件的功能进行增强，然后返回一个增强后的新组件。

// import type { RootState } from "@/store/store"
// import type { FC } from "react"
// import { useSelector } from "react-redux"


// const withAuth = (Component: FC) => {

//     return (props: any) => {
//         const adminInfo = useSelector((state: RootState) => state.admin.adminInfo);
//         console.log(JSON.stringify(adminInfo?.roleId?.permissions).includes(props.children));
//         console.log(props.children);

//         if (JSON.stringify(adminInfo.roleId.permissions).includes(props.children)) {
//             return (
//                 //返回增强后的新组件标签
//                 <Component {...props}></Component>
//             )
//         } else {
//             return null;
//         }
//     }

// }

// export default withAuth

import type { RootState } from "@/store/store";
import type { FC } from "react";
import { useSelector } from "react-redux";

const withAuth = (Component: FC) => {
    return (props: any) => {
        // 判断当前用户的权限
        const adminInfo = useSelector((state: RootState) => state.admin.adminInfo);
        if (JSON.stringify(adminInfo?.roleId?.permissions).includes(props.children)) {
            return (
                // 增强后的新组件的标签
                <Component {...props} />
            )
        }
        return null;
    }
}

export default withAuth;